<?php
!defined('IN_FRAME') && die('404 Page');
class c_cat extends Controller {
    
    private $arrCurManager;
    private $mdlManager;
    private $mdlAcl;
    private $mdlCat;
    
    /**
     * 构造函数
     */
    public function __construct() {
        $this->mdlAcl = Helper::loadModel('acl');
        $this->mdlCat = Helper::loadModel('cat');
        $this->mdlManager = Helper::loadModel('manager');
        if (!$this->mdlManager->logined()) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=login', '请先登录!');
        } else {
            $this->arrCurManager = $this->mdlManager->getSession();
        }
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'cat', 'view')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin', '你无权查看此模块!');
        }
    }
    
    public function clean() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'cat', 'clean')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=cat', '你无权删除数据!');
        }
        $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
        $strMsg = '参数错误!';
        if ($id > 0) {
            $_id = '_' . $id; 
            $objTreeList = $this->mdlCat->objTreeList;
            if (isset($objTreeList->arrIdAll[$_id]) && $this->mdlCat->delete($id)) {
                $item = $objTreeList->arrIdAll[$_id];
                $this->mdlCat->update(array('parent_id' => $item['parent_id']), array('parent_id' => $id));
                $mdlContent = Helper::loadModel('content');
                $mdlContent->update(array('cat_id' => $item['parent_id']), array('cat_id' => $id));
                $strMsg = '删除成功!';
            }
        }
        Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=cat', $strMsg);
    }
    
    public function sort() {
        $arrRe = array(
            'ok' => 'no',
            'msg' => '参数错误!'
        );
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'cat', 'sort')) {
            $arrRe['msg'] = '你无权排序!';
        } elseif (isset($_POST['id']) && isset($_POST['val']) && is_numeric($_POST['val'])) {
            $id = substr($_POST['id'], 3);
            if (is_numeric($id)) {
                $id = intval($id);
                $val = intval($_POST['val']);
                if ($id > 0 && $val >= 0) {
                    $this->mdlCat->update(array('list_order' => $val), $id);
                    $arrRe['ok'] = 'yes';
                    $arrRe['msg'] = '';
                }
            }
        }
        die(json_encode($arrRe));
    }
    
    public function index() {
        $objTreeList = $this->mdlCat->objTreeList;
        foreach ($objTreeList->arrIdAll as $strTemp => $arrTemp) {
            $objTreeList->arrIdAll[$strTemp]['edit_btn'] = '<a class="btn" href="' . HTTP_URL . '/index.php?m=admin&c=cat&a=edit&id=' . $arrTemp['id'] . '">编辑栏目</a>';
            $objTreeList->arrIdAll[$strTemp]['status'] = '<i class="icon-' . ($arrTemp['rec_status'] ? 'ok' : 'remove') . '"></i>';
            $objTreeList->arrIdAll[$strTemp]['list_btn'] = '<a class="btn" href="' . HTTP_URL . '/index.php?m=admin&c=content&cat_id=' . $arrTemp['id'] . '">内容列表</a>';
            $objTreeList->arrIdAll[$strTemp]['add_btn'] = '<a class="btn btn-primary" href="' . HTTP_URL . '/index.php?m=admin&c=content&a=add&cat_id=' . $arrTemp['id'] . '">添加内容</a>';
        }
        $objTreeList->strItem = '
        	<tr>
        		<td>{$id}</td>
        		<td>{$strSep}{$name}</td>
        		<td>{$name_en}</td>
        		<td><input style=\"margin-bottom:0px;\" title=\"正整数,小的排序在前,即时保存\" type=\"text\" id=\"id_{$id}\" placeholder=\"{$list_order}\" value=\"\" class=\"cls_order input-mini\" /></td>
        		<td>{$status}</td>
        		<td><small>{$created_at}</small></td>
        		<td>{$edit_btn}</td>
        		<td>{$list_btn}</td>
        		<td>{$add_btn}</td>
    		</tr>
    	';
        $strHtml = '<table class="table table-bordered table-condensed" width="100%" cellspacing="0" cellpadding="0">
        	<tr>
        		<td colspan="9">
        			<ul class="breadcrumb" style="margin:0px;">
        				<li>栏目列表</li>
        			</ul>
        		</td>
        	</tr>
        	<tr>
        		<th>ID</th>
        		<th>栏目名</th>
        		<th>栏目名(英文)</th>
        		<th style="width:75px;">排序</th>
        		<th style="width:31px;">状态</th>
        		<th style="width:116px;">创建时间</th>
        		<th style="width:83px;">操作</th>
        		<th style="width:83px;">列表</th>
        		<th style="width:83px;">添加</th>
    		</tr>
    	';
        $strHtml .= $objTreeList->genHtml() . '</table>';
        $arrAssign = array(
            'strTitle' => '栏目列表',
            'strTable' => $strHtml
        );
        $this->_display('admin/cat_index.php', $arrAssign);
    }
    
    public function edit() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'cat', 'update')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=cat', '你无权编辑!');
        }
        $objTreeList = $this->mdlCat->objTreeList;
        $arrAssign = array(
            'strTitle' => '编辑栏目',
            'blnAdd' => false,
            'objTreeList' => $objTreeList,
            'arrData' => array(),
            'strMsg' => '',
            'strOk' => ''
        );
        $strMsg = '';
        $id = (isset($_GET['id']) && is_numeric($_GET['id'])) ? intval($_GET['id']) : 0;
        $arrData = array();
        if ($id > 0 && isset($objTreeList->arrIdAll['_' . $id])) {
            $arrData = $objTreeList->arrIdAll['_' . $id];
            $arrAssign['id'] = $id;
        }
        if (!$arrData) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=cat', '参数错误!');
        } else {
            $arrAssign['arrData'] = $arrData;
        }
        if (isset($_POST['sub_btn'])) {
            $bln = true;
            if (!isset($_POST['parent_id']) || $_POST['parent_id'] < 0) {
                $_POST['parent_id'] = 0;
            }
            $_POST['rec_status'] = isset($_POST['rec_status']) ? 1 : 0;
            if ($_POST['rec_status'] == 0) {
                if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'cat', 'delete')) {
                    $bln = false;
                    $strMsg = '你无权设置为不可用!';
                }
            }
            if ($bln && (!isset($_POST['name']) || !$this->mdlCat->isName($_POST['name']))) {
                $bln = false;
                $strMsg = '栏目名由1~20个字符组成!';
            }
            if ($bln && $_POST['name'] != $arrData['name'] && $this->mdlCat->getOne(array('name' => $_POST['name']))) {
                $bln = false;
                $strMsg = '栏目名已存在!';
            }
            if ($bln && (!isset($_POST['name_en']) || !$this->mdlCat->isNameEn($_POST['name_en']))) {
                $bln = false;
                $strMsg = '栏目英文由1~20个数字或小写字母组成!';
            }
            if ($bln && $_POST['name_en'] != $arrData['name_en'] && $this->mdlCat->getOne(array('name_en' => $_POST['name_en']))) {
                $bln = false;
                $strMsg = '栏目英文名已存在!';
            }
            if ($bln) {
                $arrUpdate = array(
                    'rec_status' => $_POST['rec_status'],
                    'name' => $_POST['name'],
                    'name_en' => $_POST['name_en']
                );
                isset($_POST['seo_t']) && $arrUpdate['seo_t'] = $_POST['seo_t'];
                isset($_POST['seo_k']) && $arrUpdate['seo_k'] = $_POST['seo_k'];
                isset($_POST['seo_d']) && $arrUpdate['seo_d'] = $_POST['seo_d'];
                if ($bln && $_POST['parent_id'] != $arrData['parent_id']) {
                    if ($_POST['parent_id'] > 0) {
                        $strTemp = '_' . $_POST['parent_id'];
                        if (!isset($objTreeList->arrIdAll[$strTemp])) {
                            $bln = false;
                            $strMsg = '父级参数错误,请检查!';
                        } elseif ($objTreeList->arrIdAll[$strTemp]['rec_status'] == 0) {
                            $bln = false;
                            $strMsg = '父级现不可用,请检查!';
                        } elseif ($_POST['parent_id'] == $id) {
                            $bln = false;
                            $strMsg = '不能将自身作为父级!';
                        } elseif (isset($objTreeList->arrIdChildren['_' . $id]) && in_array($strTemp, $objTreeList->arrIdChildren['_' . $id])) {
                            $bln = false;
                            $strMsg = '不能将子孙级作为父级!';
                        }
                    }
                    if ($bln) {
                        $arrUpdate['parent_id'] = $_POST['parent_id'];
                    }
                }
                if ($bln && $this->mdlCat->update($arrUpdate, $id)) {
                    $arrAssign['strOk'] = '更新成功!';
                    $arrAssign['objTreeList'] = $this->mdlCat->objTreeList;
                    $arrAssign['arrData'] = $this->mdlCat->getOne($id);
                }
            }
            $arrAssign['strMsg'] = $strMsg;
        }
        $this->_display('admin/cat_edit.php', $arrAssign);
    }
    
    public function add() {
        if (!$this->mdlAcl->checkPermission($this->arrCurManager['group_name'], 'cat', 'add')) {
            Helper::jsLoadTo(HTTP_URL . '/index.php?m=admin&c=cat', '你无权添加!');
        }
        $objTreeList = $this->mdlCat->objTreeList;
        $arrAssign = array(
            'strTitle' => '添加栏目',
            'blnAdd' => true,
            'objTreeList' => $objTreeList,
            'arrData' => array(),
            'strMsg' => '',
            'strOk' => ''
        );
        if (isset($_POST['sub_btn'])) {
            $bln = true;
            $strMsg = '';
            if (!isset($_POST['parent_id']) || $_POST['parent_id'] < 0) {
                $_POST['parent_id'] = 0;
            }
            if (!isset($_POST['name']) || !$this->mdlCat->isName($_POST['name'])) {
                $bln = false;
                $strMsg = '栏目名由1~20个字符组成!';
            }
            if ($bln && $this->mdlCat->getOne(array('name' => $_POST['name']))) {
                $bln = false;
                $strMsg = '栏目名已存在!';
            }
            if ($bln && (!isset($_POST['name_en']) || !$this->mdlCat->isNameEn($_POST['name_en']))) {
                $bln = false;
                $strMsg = '栏目英文由1~20个数字或小写字母组成!';
            }
            if ($bln && $this->mdlCat->getOne(array('name_en' => $_POST['name_en']))) {
                $bln = false;
                $strMsg = '栏目英文名已存在!';
            }
            $arrInsert = array(
                'name' => $_POST['name'],
                'name_en' => $_POST['name_en'],
                'parent_id' => 0
            );
            isset($_POST['seo_t']) && $arrInsert['seo_t'] = $_POST['seo_t'];
            isset($_POST['seo_k']) && $arrInsert['seo_k'] = $_POST['seo_k'];
            isset($_POST['seo_d']) && $arrInsert['seo_d'] = $_POST['seo_d'];
            if ($bln && $_POST['parent_id'] > 0) {
                if (!isset($objTreeList->arrIdAll['_' . $_POST['parent_id']])) {
                    $bln = false;
                    $strMsg = '父级参数错误,请检查!';
                } else {
                    $arrTemp = $objTreeList->arrIdAll['_' . $_POST['parent_id']];
                    if ($arrTemp['rec_status'] == 0) {
                        $bln = false;
                        $strMsg = '父级现不可用,请检查!';
                    } else {
                        $arrInsert['parent_id'] = $arrTemp['id'];
                    }
                }
            }
            if ($bln) {
                $newId = $this->mdlCat->insert($arrInsert);
                if ($newId) {
                    $this->mdlCat->update(array('list_order' => $newId), $newId);
                    $arrAssign['strOk'] = '栏目添加成功!';
                    $arrAssign['objTreeList'] = $this->mdlCat->objTreeList;
                } else {
                    $strMsg = '未能保存,请检查!';
                }
            }
            $arrAssign['strMsg'] = $strMsg;
        }
        $this->_display('admin/cat_edit.php', $arrAssign);
    }
}